﻿package com.rbac.system.mapper;

import com.rbac.system.entity.SystemConfig;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 系统配置Mapper
 */
@Mapper
public interface SystemConfigMapper {
    
    /**
     * 根据ID查询
     */
    @Select("SELECT * FROM system_config WHERE id = #{id} AND deleted = 0")
    SystemConfig selectById(Long id);
    
    /**
     * 根据配置键查询
     */
    @Select("SELECT * FROM system_config WHERE config_key = #{configKey} AND deleted = 0")
    SystemConfig selectByKey(String configKey);
    
    /**
     * 查询所有配置
     */
    @Select("SELECT * FROM system_config WHERE deleted = 0 ORDER BY config_group, sort")
    List<SystemConfig> selectAll();
    
    /**
     * 根据分组查询配置
     */
    @Select("SELECT * FROM system_config WHERE config_group = #{configGroup} AND deleted = 0 ORDER BY sort")
    List<SystemConfig> selectByGroup(String configGroup);
    
    /**
     * 根据状态查询配置
     */
    @Select("SELECT * FROM system_config WHERE status = #{status} AND deleted = 0 ORDER BY config_group, sort")
    List<SystemConfig> selectByStatus(Integer status);
    
    /**
     * 插入配置
     */
    @Insert("INSERT INTO system_config (config_key, config_value, config_name, description, config_type, " +
            "config_group, is_system, status, sort, create_time, update_time, deleted) " +
            "VALUES (#{configKey}, #{configValue}, #{configName}, #{description}, #{configType}, " +
            "#{configGroup}, #{isSystem}, #{status}, #{sort}, #{createTime}, #{updateTime}, #{deleted})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SystemConfig systemConfig);
    
    /**
     * 更新配置
     */
    @Update("UPDATE system_config SET config_value = #{configValue}, config_name = #{configName}, " +
            "description = #{description}, config_type = #{configType}, config_group = #{configGroup}, " +
            "status = #{status}, sort = #{sort}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateById(SystemConfig systemConfig);
    
    /**
     * 更新配置值
     */
    @Update("UPDATE system_config SET config_value = #{configValue}, update_time = #{updateTime} " +
            "WHERE config_key = #{configKey}")
    int updateValueByKey(@Param("configKey") String configKey, 
                        @Param("configValue") String configValue, 
                        @Param("updateTime") java.time.LocalDateTime updateTime);
    
    /**
     * 删除配置
     */
    @Update("UPDATE system_config SET deleted = 1, update_time = #{updateTime} WHERE id = #{id}")
    int deleteById(@Param("id") Long id, @Param("updateTime") java.time.LocalDateTime updateTime);
    
    /**
     * 检查配置键是否存在
     */
    @Select("SELECT COUNT(*) FROM system_config WHERE config_key = #{configKey} AND deleted = 0")
    int countByKey(String configKey);
}


